﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Hired
{
    public partial class settings : System.Web.UI.Page
    {
        private CompanyAccount ca;
        private EmployerPassport ep;
        private JobseekerAccount jsa;

        protected void Page_Load(object sender, EventArgs e)
        {
            lblUsername.Text = (string)(Session["username"]);

            if (User.IsInRole("company"))
            {
                ca = DBCompanyAccount.RetrieveCompanyAccount(User.Identity.Name, false);

                #region Initializing values
                lblEmail.Text = ca.Email;
                ddlSecurityQuestion.Text = ca.SecurityQuestion;
                txtSecurityAnswer.Text = ca.SecurityAnswer;
                if ((bool)ca.AllowMessages)
                {
                    cbAllowMessages.Checked = true;
                }
                if ((bool)ca.AllowNotifications)
                {
                    cbAllowNotifications.Checked = true;
                }
                ddlPrivacySettings.SelectedIndex = (int)ca.PrivacySettings;
                #endregion

                pnlEmployers.Visible = true;
            }
            else if (User.IsInRole("employer"))
            {
                ep = DBEmployerPassport.RetrieveEmployerPassport(User.Identity.Name);
                ca = DBCompanyAccount.RetrieveCompanyAccount(ep.CAccountID, false);

                #region Initializing values
                lblEmail.Text = ep.Email;
                ddlSecurityQuestion.Text = ep.SecurityQuestion;
                txtSecurityAnswer.Text = ep.SecurityAnswer;
                if ((bool)ca.AllowMessages)
                {
                    cbAllowMessages.Checked = true;
                }
                if ((bool)ca.AllowNotifications)
                {
                    cbAllowNotifications.Checked = true;
                }
                ddlPrivacySettings.SelectedIndex = (int)ca.PrivacySettings;
                #endregion

                if ((bool)ep.Administrator)
                {
                    pnlEmployers.Visible = true;
                }
            }
            else if (User.IsInRole("jobseeker"))
            {
                jsa = DBJobseekerAccount.retrieveJobseekerAccount(User.Identity.Name, false);

                #region Initializing values
                lblEmail.Text = jsa.Email;
                ddlSecurityQuestion.Text = jsa.SecurityQuestion;
                txtSecurityAnswer.Text = jsa.SecurityAnswer;
                if ((bool)jsa.AllowMessages)
                {
                    cbAllowMessages.Checked = true;
                }
                if ((bool)jsa.AllowNotifications)
                {
                    cbAllowNotifications.Checked = true;
                }
                ddlPrivacySettings.SelectedIndex = (int)jsa.PrivacySettings;
                #endregion
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (IsValid)
            {
                if (ca != null && ep == null)
                {
                    if (txtNewPassword.Text != "")
                    {
                        ca.Password = Encryption.md5Encrypt(txtNewPassword.Text);
                    }
                    #region Setting values
                    ca.SecurityQuestion = ddlSecurityQuestion.Text;
                    ca.SecurityAnswer = txtSecurityAnswer.Text;
                    if (cbAllowMessages.Checked)
                    {
                        ca.AllowMessages = true;
                    }
                    else
                    {
                        ca.AllowMessages = false;
                    }

                    if (cbAllowNotifications.Checked)
                    {
                        ca.AllowNotifications = true;
                    }
                    else
                    {
                        ca.AllowNotifications = false;
                    }
                    ca.PrivacySettings = ddlPrivacySettings.SelectedIndex;
                    #endregion

                    DBCompanyAccount.UpdateCompanyAccount(ca);
                }
                else if (ep != null)
                {
                    if (txtNewPassword.Text != "")
                    {
                        ep.Password = Encryption.md5Encrypt(txtNewPassword.Text);
                    }

                    #region Setting values
                    ep.SecurityQuestion = ddlSecurityQuestion.Text;
                    ep.SecurityAnswer = txtSecurityAnswer.Text;
                    if (cbAllowMessages.Checked)
                    {
                        ca.AllowMessages = true;
                    }
                    else
                    {
                        ca.AllowMessages = false;
                    }

                    if (cbAllowNotifications.Checked)
                    {
                        ca.AllowNotifications = true;
                    }
                    else
                    {
                        ca.AllowNotifications = false;
                    }
                    ca.PrivacySettings = ddlPrivacySettings.SelectedIndex;
                    #endregion

                    DBCompanyAccount.UpdateCompanyAccount(ca);
                    DBEmployerPassport.UpdateEmployerPassport(ep);
                }
                else if (jsa != null)
                {
                    if (txtNewPassword.Text != "")
                    {
                        jsa.Password = Encryption.md5Encrypt(txtNewPassword.Text);
                    }

                    #region Setting values
                    jsa.SecurityQuestion = ddlSecurityQuestion.Text;
                    jsa.SecurityAnswer = txtSecurityAnswer.Text;
                    if (cbAllowMessages.Checked)
                    {
                        jsa.AllowMessages = true;
                    }
                    else
                    {
                        jsa.AllowMessages = false;
                    }

                    if (cbAllowNotifications.Checked)
                    {
                        jsa.AllowNotifications = true;
                    }
                    else
                    {
                        jsa.AllowNotifications = false;
                    }
                    jsa.PrivacySettings = ddlPrivacySettings.SelectedIndex;
                    #endregion

                    DBJobseekerAccount.updateJobseekerAccount(jsa);
                }

                lblConfirmation.Text = "Your settings have been updated.";
            }
        }


        protected void btnDeleteAccount_Click(object sender, EventArgs e)
        {
            if (IsValid)
            {
                Session["delete"] = true;
                Server.Transfer("deleteaccount.aspx");
            }
        }

        protected void CustomUpdatePassword_ServerValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = false;

            if (ca != null)
            {
                if (Encryption.verify(txtPassword.Text, ca.Password))
                {
                    args.IsValid = true;
                }
            }
            else if (ep != null)
            {
                if (Encryption.verify(txtPassword.Text, ep.Password))
                {
                    args.IsValid = true;
                }
            }
            else if (jsa != null)
            {
                if (Encryption.verify(txtPassword.Text, jsa.Password))
                {
                    args.IsValid = true;
                }
            }
        }

        protected void CustomDeletePassword_ServerValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = false;

            if (ca != null && ep == null)
            {
                if (Encryption.verify(txtDeletePassword.Text, ca.Password))
                {
                    args.IsValid = true;
                }
            }
            else if (ep != null)
            {
                if (Encryption.verify(txtDeletePassword.Text, ep.Password))
                {
                    args.IsValid = true;
                }
            }
            else if (jsa != null)
            {
                if (Encryption.verify(txtDeletePassword.Text, jsa.Password))
                {
                    args.IsValid = true;
                }
            }
        }
    }
}